package com.hg.library.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;

/**
 * Created by ASUS-PC on 2016/1/28.
 */
public class AnimUtils {

    public static final int X = 0;

    public static final int Y = 1;

    public static final int X_Y = 2;

    public static final int DURATION = 150;

    private AnimUtils() {
    }

    public static Animator scaleAnim(final View v, int duration, float fromScale, float toScale) {
        return mScaleAnim(v, duration, fromScale, toScale, X_Y, null);
    }

    public static Animator scaleAnimX(final View v, int duration, float fromScale, float toScale) {
        return mScaleAnim(v, duration, fromScale, toScale, X, null);
    }

    public static Animator scaleAnimY(final View v, int duration, float fromScale, float toScale) {
        return mScaleAnim(v, duration, fromScale, toScale, Y, null);
    }

    public static Animator scaleAnim(final View v, int duration, float fromScale, float toScale, AnimatorListenerAdapter animatorListenerAdapter) {
        return mScaleAnim(v, duration, fromScale, toScale, X_Y, animatorListenerAdapter);
    }

    public static Animator scaleAnimX(final View v, int duration, float fromScale, float toScale, AnimatorListenerAdapter animatorListenerAdapter) {
        return mScaleAnim(v, duration, fromScale, toScale, X, animatorListenerAdapter);
    }

    public static Animator scaleAnimY(final View v, int duration, float fromScale, float toScale, AnimatorListenerAdapter animatorListenerAdapter) {
        return mScaleAnim(v, duration, fromScale, toScale, Y, animatorListenerAdapter);
    }

    public static Animator AlphaAnim(final View v, int duration, float fromAlpha, float toAlpha) {
        return mAlphaAnim(v, duration, fromAlpha, toAlpha, null);
    }

    public static Animator AlphaAnim(final View v, int duration, float fromAlpha, float toAlpha, AnimatorListenerAdapter animatorListenerAdapter) {
        return mAlphaAnim(v, duration, fromAlpha, toAlpha, animatorListenerAdapter);
    }

    public static void scaleAnimXY(final View v, int duration, float fromScaleX, float toScaleX, float fromScaleY, float toScaleY) {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scaleAnimX(v, duration, fromScaleX, toScaleX)).with(scaleAnimY(v, duration, fromScaleY, toScaleY));
        animatorSet.start();
    }

    private static Animator mAlphaAnim(final View v, int duration, float fromAlpha, float toAlpha, AnimatorListenerAdapter animatorListenerAdapter) {
        return AnimHelper(v, duration, fromAlpha, toAlpha, animatorListenerAdapter, new AnimHelperListener() {
            @Override
            public void upDataValue(float value) {
                v.setAlpha(value);
            }
        });
    }

    private static Animator mScaleAnim(final View v, int duration, float fromScale, float toScale, final int type
            , AnimatorListenerAdapter animatorListenerAdapter) {

        return AnimHelper(v, duration, fromScale, toScale, animatorListenerAdapter, new AnimHelperListener() {
            @Override
            public void upDataValue(float value) {
                if (type == X_Y) {
                    v.setScaleX(value);
                    v.setScaleY(value);
                } else if (type == X) {
                    v.setScaleX(value);
                } else if (type == Y) {
                    v.setScaleY(value);
                }
            }
        });
    }

    private static Animator AnimHelper(final View v, int duration, float fromValue, float toValue
            , AnimatorListenerAdapter animatorListenerAdapter, final AnimHelperListener helperListener) {

        ObjectAnimator animator = ObjectAnimator.ofFloat("", "", fromValue, toValue);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                helperListener.upDataValue((Float) animation.getAnimatedValue());
            }
        });
        if (animatorListenerAdapter != null)
            animator.addListener(animatorListenerAdapter);
        if (duration >= 0)
            animator.setDuration(duration);
        animator.start();
        return animator;
    }

    private interface AnimHelperListener {
        void upDataValue(float value);
    }
}
